package priv.dajie.tzzq.test;

import priv.dajie.tzzq.bot.Bot;
import priv.dajie.tzzq.bot.BotManager;
import priv.dajie.tzzq.bot.model.BuyState;
import priv.dajie.tzzq.bot.model.PackItem;
import priv.dajie.tzzq.bot.model.PackItems;
import priv.dajie.tzzq.bot.model.SellItem;
import priv.dajie.tzzq.bot.steam.SteamBot;
import priv.dajie.tzzq.common.ApplicationContextUnit;
import priv.dajie.tzzq.model.BuyGoods;
import priv.dajie.tzzq.model.GoodsState;
import priv.dajie.tzzq.model.TradeStateItem;
import priv.dajie.tzzq.model.TradeStateItems;
import priv.dajie.tzzq.service.TradeService;
import priv.dajie.tzzq.spider.model.Platform;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @ClassName BuffBugFix
 * @Description
 * @Author dajie
 * @Date 2019/10/10 11:22 下午
 * @Version V1.0
 **/
public class BuffBugFix {

    private static TradeService tradeService = (TradeService) ApplicationContextUnit.getInstance().getBean("tradeService");

    public static void main(String[] args) throws InterruptedException {
        for (int i=0;i<5;i++){
            fix();
        }
    }

    public static void fix() throws InterruptedException {
        Bot bot = BotManager.getC5Bot();
        SteamBot steamBot = BotManager.getSteamBot();
        PackItems pack = bot.getPack();
        TradeStateItems tradeStateItems = tradeService.listTradeStateItem(GoodsState.WAIT);
        tradeStateItems.addAll(tradeService.listTradeStateItem(GoodsState.PACKAGE));
        tradeStateItems.addAll(tradeService.listTradeStateItem(GoodsState.CONFIRMING));
        tradeStateItems.addAll(tradeService.listTradeStateItem(GoodsState.SELLING));
        List<PackItem> takeBack = new ArrayList<>();
        HashSet<String> set = new HashSet<>();
        r:for (PackItem packItem : pack){
            String steamName = packItem.getSteamName();
            for(TradeStateItem tradeStateItem :tradeStateItems){
                if (tradeStateItem.getSteamName().equals(steamName)){
                    continue r;
                }
            }
            if (set.contains(packItem.getSteamName())){
                System.out.println("商品已存在");
                continue ;
            }
            
            if(packItem.isException() ){
                continue;
            }
            Thread.sleep(2000);
            SellItem sellItem = bot.getSellItemsBySteamName(packItem.getSteamName()).get(0);
            if (sellItem.getPrice().compareTo(new BigDecimal("1"))>=0){
                System.out.println("商品价值过高:"+sellItem);
                continue ;
            }

            takeBack.add(packItem);
            set.add(packItem.getSteamName());
            System.out.println("发现可出售商品:"+packItem);
            System.out.println("购买价格:" + sellItem);
            if (takeBack.size()>=20){
                break ;
            }

        }
        int i = 0;
        while (true){
            int max = 0;
            if(i+5>=takeBack.size()){
                max=takeBack.size();
            }
            else {
                max= i+5;
            }
            while (bot.isTrading()){
                Thread.sleep(2000);
                System.out.println("等待交易完成");
            }
            String[] tradeIds = bot.autoTakeBack(takeBack.subList(i, max));
            for(String id : tradeIds){

                String successId;
                while (true){
                    try {
                        successId = steamBot.trade(id);
                    }catch (Exception e){
                        Thread.sleep(1000);
                        continue;
                    }

                    break;
                }

                List<String> steamNames = steamBot.getTradeItems(successId);
                for(String steamName : steamNames){
                    tradeService.addTrade(new BuyGoods(steamName, new BigDecimal("0.01"), Platform.C5, "666", BuyState.WAIT ,new BigDecimal("0.02")));
                    System.out.println(steamName+"取回成功");
                }
                TradeStateItems stateItems = tradeService.listTradeStateItem(GoodsState.WAIT);
                for(TradeStateItem tradeState : stateItems){
                    if ("666".equals(tradeState.getBuyId())){
                        tradeService.updateState(tradeState.getId(), GoodsState.PACKAGE);
                        System.out.println("待上架："+tradeState);
                    }
                }
            }
            i=max;
            if(i>=takeBack.size()){
                break;
            }
        }
    }
}
